﻿using System;
using System.Reflection;
using System.IO;

namespace PingPong.Generic.Helpers
{

	public static class ResourceHelper
	{
		#region Members

		/// <summary>
		/// The current assembly.
		/// </summary>
		private static  Lazy<Assembly> assembly;

		#endregion

		/// <summary>
		/// Gets the currently executing assembly.
		/// </summary>
		/// <value>The assembly.</value>
		private static Assembly CurrentAssembly
		{
			get
			{
				return assembly.Value;
			}
		}

		#region Constructor

		static ResourceHelper()
		{
			assembly = new Lazy<Assembly>(() => Assembly.GetEntryAssembly());
		}

		#endregion

		#region Methods

		/// <summary>
		/// Gets a text resource.
		/// </summary>
		/// <returns>The text resource.</returns>
		/// <param name="resourceName">Resource name.</param>
		public static string GetTextResource(string resourceName)
		{
			try
			{
				var stream = CurrentAssembly.GetManifestResourceStream(resourceName);
				TextReader textReader = new StreamReader(stream);
				return textReader.ReadToEnd();
			}
			catch
			{
				return string.Empty;
			}
		}

		#endregion
	}
}

